/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 27/10/2004
 *
 */

package au.com.lastweekend.jim.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.swing.tree.DefaultTreeModel;

import org.apache.log4j.Logger;

import au.com.lastweekend.jim.imagebase.ImageBase;
import au.com.lastweekend.jim.swing.SwingUtil;
import au.com.lastweekend.jim.swing.tree.MutableTreeModel;

/**
 * This model is shared by the KeywordSearchPanel and KeywordMetadata trees
 * 
 * The tree nodes are able to respond to isSearchSelected() and getTriStateOfSelectedImages() the nodes
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: KeywordTreeModel.java,v 1.6 2006/02/19 06:04:54 ggardner Exp $
 */
public class KeywordTreeModel extends DefaultTreeModel implements MutableTreeModel {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(KeywordTreeModel.class);
    private ImageBase imageBase;

    /**
     * @return
     */
    public static String getImageBaseString(Keyword node) {

        StringBuffer buff = new StringBuffer();
        boolean needsSeparator = false;
        Object[] nodePath = SwingUtil.getPathToRoot(node, 0);

        for (Object pathObject : nodePath) {
            Keyword keyword = (Keyword) pathObject;
            if (keyword.getName() == null) {
                // this should be root
                continue;
            } else {
                if (needsSeparator) {
                    buff.append(ImageBase.KEYWORD_SEPARATOR_CHAR);
                } else {
                    needsSeparator = true;
                }
                buff.append(keyword.getName());
            }

        }

        return buff.toString();
    }

    public static Keyword buildRoot(Set<String> storedKeywords, char keywordSeparator) {

        List<String> sortedKeywords = new ArrayList<String>(storedKeywords);
        Collections.sort(sortedKeywords);

        Keyword root = new Keyword(null, true);

        for (String keywordPath : sortedKeywords) {

            String[] keywordParts = keywordPath.split(String.valueOf(keywordSeparator));
            Keyword node = root;
            for (String keywordName : keywordParts) {
                if (node.hasChild(keywordName)) {
                    node = node.getChild(keywordName);
                } else {
                    Keyword newChild = new Keyword(keywordName);
                    node.insert(newChild, node.getChildCount());
                    node = newChild;
                }
            }

            LOG.debug("Built keyword nodes for " + keywordPath);
        }

        return root;
    }

    public KeywordTreeModel(ImageBase imageBase) {

        super(null);
        this.imageBase = imageBase;
        setRoot(buildRoot());

    }

    /**
     * @return
     */
    public Keyword buildRoot() {

        Set<String> storedKeywords = getImageBase().getStoredKeywords();
        return buildRoot(storedKeywords, ImageBase.KEYWORD_SEPARATOR_CHAR);

    }

    /**
     * @return
     */
    private ImageBase getImageBase() {

        return imageBase;
    }

}